package com.clinic.logic.usercontact;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.clinic.accessor.db.UserAccessor;
import com.clinic.accessor.db.UserContactAccessor;
import com.clinic.entity.db.User;
import com.clinic.entity.db.UserContact;
import com.clinic.entity.gui.GuiUser;
import com.clinic.entity.gui.GuiUserContact;

@Component
public class UserContactLogic {
	
	@Autowired
	private UserContactAccessor userContactAccessor;
	
	@Autowired
	private UserAccessor userAccessor;
	
	public GuiUserContact getUserContactDetailsByUserId(long id) {
		
		User user = null;
		UserContact userContact = userContactAccessor.getUserContactByUserId(id);
		
		if(userContact != null) {
			user = userContact.getUser();
			} else {
			user = userAccessor.getUserById(id);
			}
		return (getGuiUserContactByUserContact(userContact, user));
	}

	private GuiUserContact getGuiUserContactByUserContact(UserContact userContact, User user) {
		
		GuiUserContact guiUserContact = new GuiUserContact();
		
		if(userContact != null) {
			guiUserContact.setContactId(userContact.getContactId());
			guiUserContact.setTelNo(userContact.getTelNo());
			guiUserContact.setAddress(userContact.getAddress());
		}
		
		if(user != null) {
			GuiUser guiUser = new GuiUser();
			guiUser.setUserId(user.getUserId());
			guiUser.setName(user.getName());
			guiUser.setType(user.getType().toString());
			guiUser.setAdmin(user.isAdmin());
			guiUserContact.setUser(guiUser);		
		}
		return guiUserContact;
	}
}
